package com.zzyweb.rentalcarsys.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.IdUtil;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.zzyweb.rentalcarsys.entity.UserAccount;
import com.zzyweb.rentalcarsys.exception.BusinessException;
import com.zzyweb.rentalcarsys.exception.ErrorCode;
import com.zzyweb.rentalcarsys.mapper.UserAccountMapper;
import com.zzyweb.rentalcarsys.service.CaptchaService;
import com.zzyweb.rentalcarsys.utils.EmailApi;
import com.zzyweb.rentalcarsys.utils.RedisUtil;
import jakarta.annotation.Resource;
import org.apache.ibatis.builder.BuilderException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author zhiyi
 * @date 9/29/25
 **/
@Service
public class CaptchaServiceImpl implements CaptchaService {
    @Resource
    DefaultKaptcha defaultKaptcha;
    @Resource
    EmailApi emailApi;
    @Resource
    RedisUtil redisUtil;
    @Autowired
    UserAccountMapper userAccountMapper;
    @Override
    public void sendEmailCaptcha(String to) {
        //检查邮箱状态
        if (redisUtil.get(to) != null && redisUtil.get(to).equals("banned")) {
            throw new BusinessException(ErrorCode.BANNED_EMAIL);
        }
        String captchaCode ="";
        //检查是否已经有有效的验证码
        String s = redisUtil.get(to);
        if (s == null) {
            //没有发送过验证码
            //
            captchaCode = defaultKaptcha.createText();
            redisUtil.set(to,captchaCode);
            redisUtil.set(captchaCode,String.valueOf(1));
        } else {
            // 拿出之前的code
            captchaCode =redisUtil.get(to);
            //重新设定有效期
            redisUtil.set(to, captchaCode);
            //增加该地址发送次数
            int count = Integer.parseInt(redisUtil.get(captchaCode)) + 1;
            if (count==11) {
                //进行暂时的封号
                redisUtil.banEmail(to);
                throw new BusinessException(ErrorCode.BANNED_EMAIL);
            }
            redisUtil.set(captchaCode, String.valueOf(count));
        }

        //邮箱发送
        if (!captchaCode.isEmpty()) {
            emailApi.sendHtmlEmail("验证码", captchaCode, to);
        }
    }

    @Override
    public void verifyEmailCaptcha(String email, String captcha) {
        String storedCaptcha = redisUtil.get(email);
        if (!storedCaptcha.equals(captcha)) {
            throw new BusinessException(ErrorCode.WRONG_CAPTCHA_CODE);
        }
    }

    @Override
    public Map<String, String> createImgCaptcha(String captchaId) {
        Map<String,String> map = new HashMap<>();
        //第一步先判定是否有captcha id
        if (redisUtil.get(captchaId) == null || !StringUtils.hasText(redisUtil.get(captchaId))) {
            // 创建captcha id
            captchaId = IdUtil.getSnowflakeNextIdStr();
        }
        //设置验证码图片长、宽、验证码字符数、干扰线宽度
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(120, 30, 4, 10);
        //设置字体样式和大小
        lineCaptcha.setFont(new Font("仿宋", Font.BOLD, 30));
        //获取验证码
        String code = lineCaptcha.getCode();
        redisUtil.set(captchaId,code,120L, TimeUnit.SECONDS);
        String imageBase64 = lineCaptcha.getImageBase64Data();

        //在有了codeId 和图案以后，一起返回给前端
        map.put("captchaId", captchaId);
        map.put("captchaUrl", imageBase64);
        return map;
    }

    @Override
    public void verifyImgCaptcha(String captchaCode, String captchaId) {
        String code = redisUtil.get(captchaId);
        if (!code.equals(captchaCode)) {
            throw new BusinessException(ErrorCode.WRONG_CODE);
        }
    }


}
